package swingxrcp.framework.server.action;

import java.io.IOException;
import java.io.Serializable;
import java.util.List;

import swingxrcp.framework.model.Action;

/**
 * Action处理器
 *
 * @author liangfei
 *
 */
public class ActionProcesser {

	protected final ActionFactory actionFactory;

	protected final ActionInterceptor actionInterceptor;

	public ActionProcesser(ActionFactory actionFactory, List<ActionInterceptor> actionInterceptors) {
		this.actionFactory = actionFactory;
		this.actionInterceptor = ActionInterceptorChain.reduceActionInterceptorChain(actionInterceptors); // 将拦截器列表归约成链
	}

	public Serializable process(String actionName, Serializable object) throws IOException {
		Action action = actionFactory.getAction(actionName); // 获取Action实例
		if (action == null)
			throw new IOException("Not existed action: " + actionName);
		Serializable result; // 返回的结果
		if (actionInterceptor != null)
			result = actionInterceptor.intercept(action, object); // 如果有拦截器，则执行拦截器
		else
			result = action.execute(object); // 否则，直接执行Action实例
		return result;
	}

}
